﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace EvidenceManagement.Data
{
    public static class ConversionHelper
    {
        /// <summary>
        /// If NULL return 0, else return the value
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static int GetInt(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? 0 : Convert.ToInt32(reader[index]);
        }

        /// <summary>
        /// Reads an column of data type int and returns the replacement value if the value is System.DBNull
        /// </summary>
        /// <param name="reader">The datareader that contains the result set to read.</param>
        /// <param name="fieldName">Name of the database column to read.</param>
        /// <param name="dBNullReplacementValue">The System.DBNull replacement value.</param>
        /// <returns></returns>
        public static int GetInt(IDataReader reader, string fieldName, int dBNullReplacementValue)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? dBNullReplacementValue : Convert.ToInt32(reader[index]);
        }

        /// <summary>
        /// Returns 0 if the <paramref name="scalarValue"/> is <value>DBNull</value>
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static int GetInt(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? 0 : Convert.ToInt32(scalarValue);
        }

        /// <summary>
        /// If NULL return empty string, else return the value
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static string GetString(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? string.Empty : Convert.ToString(reader[index]);
        }

        /// <summary>
        /// Reads an column of data type string and returns the replacement value if the value is System.DBNull
        /// </summary>
        /// <param name="reader">The datareader that contains the result set to read.</param>
        /// <param name="fieldName">Name of the database column to read.</param>
        /// <param name="dBNullReplacementValue">The System.DBNull replacement value.</param>
        /// <returns></returns>
        public static string GetString(IDataReader reader, string fieldName, string dBNullReplacementValue)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? dBNullReplacementValue : Convert.ToString(reader[index]);
        }

        /// <summary>
        /// Returns string.Empty if the <paramref name="scalarValue"/> is <value>DBNull</value>.
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static string GetString(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? string.Empty : Convert.ToString(scalarValue);
        }

        /// <summary>
        /// If NULL return 0, else return the value
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static long GetLong(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? 0 : Convert.ToInt64(reader[index]);
        }

        /// <summary>
        /// Reads an column of data type long and returns the replacement value if the value is System.DBNull
        /// </summary>
        /// <param name="reader">The datareader that contains the result set to read.</param>
        /// <param name="fieldName">Name of the database column to read.</param>
        /// <param name="dBNullReplacementValue">The System.DBNull replacement value.</param>
        /// <returns></returns>
        public static long GetLong(IDataReader reader, string fieldName, long dBNullReplacementValue)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? dBNullReplacementValue : Convert.ToInt64(reader[index]);
        }

        /// <summary>
        /// Returns 0 if the <paramref name="scalarValue"/> is <value>DBNull</value>.
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static long GetLong(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? 0 : Convert.ToInt64(scalarValue);
        }

        /// <summary>
        /// If NULL return false, else return the value
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static bool GetBoolean(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? false : Convert.ToBoolean(reader[index]);
        }

        /// <summary>
        ///  Reads an column of data type bool and returns the replacement value if the value is System.DBNull
        /// </summary>
        /// <param name="reader">The datareader that contains the result set to read.</param>
        /// <param name="fieldName">Name of the database column to read.</param>
        /// <param name="dBNullReplacementValue">The System.DBNull replacement value.</param>
        /// <returns></returns>
        public static bool GetBoolean(IDataReader reader, string fieldName, bool dBNullReplacementValue)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? dBNullReplacementValue : Convert.ToBoolean(reader[index]);
        }

        /// <summary>
        /// Returns <value>false</value> if the <paramref name="scalarValue"/> is <value>DBNull</value>.
        /// </summary>        
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static bool GetBoolean(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? false : Convert.ToBoolean(scalarValue);
        }

        /// <summary>
        /// If NULL return 0, else return the value
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static decimal GetDecimal(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? 0 : Convert.ToDecimal(reader[index]);
        }

        /// <summary>
        /// Reads an column of data type decimal and returns the replacement value if the value is System.DBNull
        /// </summary>
        /// <param name="reader">The datareader that contains the result set to read.</param>
        /// <param name="fieldName">Name of the database column to read.</param>
        /// <param name="dBNullReplacementValue">The System.DBNull replacement value.</param>
        /// <returns></returns>
        public static decimal GetDecimal(IDataReader reader, string fieldName, decimal dBNullReplacementValue)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? dBNullReplacementValue : Convert.ToDecimal(reader[index]);
        }

        /// <summary>
        ///  Returns 0 if the <paramref name="scalarValue"/> is <value>DBNull</value>.
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static decimal GetDecimal(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? 0 : Convert.ToDecimal(scalarValue);
        }

        /// <summary>
        /// Reads a column of data type UniqueIdentifier and returns a GUID
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static Guid GetGuid(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return new Guid(reader[index].ToString());
        }

        /// <summary>
        /// Returns Guid.Empty if the <paramref name="scalarValue"/> is <value>DBNull</value>.
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static Guid GetGuid(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? Guid.Empty : new Guid(Convert.ToString(scalarValue));
        }

        /// <summary>
        /// Gets the date.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <returns></returns>
        public static DateTime GetDate(IDataReader reader, string fieldName)
        {
            int index = reader.GetOrdinal(fieldName);
            return (reader.IsDBNull(index)) ? DateTime.MinValue : Convert.ToDateTime(reader[fieldName]);
        }

        /// <summary>
        /// Gets the date.
        /// </summary>
        /// <param name="scalarValue">The scalar value.</param>
        /// <returns></returns>
        public static DateTime GetDate(object scalarValue)
        {
            return (scalarValue == DBNull.Value) ? DateTime.MinValue : Convert.ToDateTime(scalarValue);
        }

    }
}
